﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PhilipSupport.Controls
{
    public partial class PageControl : UserControl
    {
        private int currentPage = 0;
        private Button previous_btn;
        private int buttonWidth = 32;
        private Button next_btn;
        private int visibleCount = 5;
        private int totalPage = 1;

        public delegate void PageIndexChangeHandler(int pageIndex);
        public event PageIndexChangeHandler PageIndexChanged;

        public PageControl()
        {
            InitializeComponent();
            var previous_btn = new Button();
            previous_btn.Text = "<上一页";
            previous_btn.Location = new Point(0, 0);
            previous_btn.Height = buttonWidth;
            previous_btn.FlatStyle = FlatStyle.Flat;
            previous_btn.FlatAppearance.BorderColor = Color.FromArgb(255, 222, 223, 224);
            previous_btn.Font = new Font("微软雅黑", 9);
            previous_btn.ForeColor = Color.FromArgb(255, 40, 39, 209);
            previous_btn.Click += Previous_btn_Click;
            this.previous_btn = previous_btn;

            var next_btn = new Button();
            next_btn.Text = "下一页>";
            this.Controls.Add(next_btn);
            next_btn.Height = buttonWidth;
            next_btn.FlatStyle = FlatStyle.Flat;
            next_btn.ForeColor = Color.FromArgb(255, 40, 39, 209);
            next_btn.Font = new Font("微软雅黑", 9);
            next_btn.FlatAppearance.BorderColor = Color.FromArgb(255, 222, 223, 224);
            next_btn.Click += Next_btn_Click;
            this.next_btn = next_btn;
        }

        public int GetCurrentPage() {
            return this.currentPage;
        }

        private void Next_btn_Click(object sender, EventArgs e)
        {
            if (this.currentPage < totalPage)
            {
                this.currentPage++;
            }
            UpdatePageInfo(totalPage, this.currentPage, visibleCount);
        }

        private void Previous_btn_Click(object sender, EventArgs e)
        {
            if (this.currentPage > 1) {
                this.currentPage--;
            }
            UpdatePageInfo(totalPage, this.currentPage, this.visibleCount);
        }

        public void UpdatePageInfo(int totalPage, int currentPage, int visibleCount) {
            this.Controls.Clear();
            this.visibleCount = visibleCount;
            this.totalPage = totalPage;
            this.currentPage = currentPage;
            
            int buttonGap = 5;
            int headButtonWidth = 80;
            
            this.Controls.Add(previous_btn);
            previous_btn.Location = new Point(0, 0);

            int startIndex = currentPage - visibleCount / 2;

            if(totalPage - currentPage < visibleCount / 2)
            {
                startIndex = totalPage - visibleCount + 1;
            }

            if (startIndex < 1)
            {
                startIndex = 1;
            }

            int count = 0;
            for (int i = startIndex; i < visibleCount + startIndex; i++) {
                if (i <= totalPage && i > 0)
                {
                    var button = new Button();
                    button.FlatStyle = FlatStyle.Flat;
                    button.FlatAppearance.BorderColor = Color.FromArgb(255, 222, 223, 224);
                    button.Font = new Font("微软雅黑", 9);
                    if (i == currentPage)
                    {

                        button.FlatAppearance.BorderSize = 0;
                        button.ForeColor = Color.Black;
                    }
                    else
                    {
                        button.ForeColor = Color.FromArgb(255, 40, 39, 209);
                    }
                    button.Width = buttonWidth;
                    button.Height = buttonWidth;
                    this.Controls.Add(button);
                    button.Click += Button_Click;
                    button.Location = new Point((i - startIndex) * (buttonWidth + buttonGap) + headButtonWidth);
                    button.Text = i.ToString();
                    count++;
                }

                PageIndexChanged?.Invoke(currentPage);
            }


            this.Controls.Add(next_btn);
            next_btn.Location = new Point((count) * (buttonWidth + buttonGap) + headButtonWidth);

            this.Width = headButtonWidth * 2 + count * (buttonWidth + buttonGap);
            this.Height = 32;
            this.Location = new Point(this.Parent.Width - this.Width, this.Location.Y);
        }

        private void Button_Click(object sender, EventArgs e)
        {
            var btn = (Button)sender;
            int currentPage = int.Parse(btn.Text);
            UpdatePageInfo(totalPage, currentPage, visibleCount);
        }
    }
}
